﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core.Repositories;
using Core.Models;
using BlogMvc.Ultities;

namespace BlogMvc.Controllers
{
    [Authorize]
    public class PostController : Controller
    {
        DatabaseEntities _db = new DatabaseEntities();

        PostRepository _storeDB;
        public PostController()
        {
            _storeDB = new PostRepository(_db);
        }

        #region ver_0.1 Code First
        ////
        //// GET: /Post/  
        //[Authorize]
        //public ActionResult Index(int pageIndex = 1)
        //{
        //    var posts = new PostRepository().Get();

        //    ViewBag.TotalPage = (int)Math.Ceiling(posts.Count() / (double)Config.PageSize);
        //    ViewBag.PageIndex = pageIndex;
        //    ViewBag.PageSizeRender = Config.PageSizeRender;

        //    int take = Config.PageSize;
        //    int skipAt = (pageIndex * take) - take;

        //    return View(posts.Skip(skipAt).Take(take).ToList());
        //}

        ////
        //// GET: /Post/Create
        //[Authorize]
        //public ActionResult Create()
        //{
        //    return View();
        //}

        ////
        //// GET: /Post/Create
        //[HttpPost]
        //[Authorize]
        //public ActionResult Create(Post post)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _storeDB.Create(post);
        //        return RedirectToAction("Index");
        //    }
        //    else
        //    {
        //        return View(post);
        //    }

        //}

        ////
        //// GET: /Post/Edit
        //[Authorize]
        //public ActionResult Edit(int id)
        //{
        //    var post = _storeDB.GetById(id);
        //    post.Content = Server.HtmlDecode(post.Content);
        //    return View(post);
        //}

        ////
        //// GET: /Post/Edit
        //[HttpPost]
        ////[ValidateInput(false)]
        //[Authorize]
        //public ActionResult Edit(Post post)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _storeDB.Edit(post);
        //        return RedirectToAction("Index");
        //    }
        //    else
        //    {
        //        return View(post);
        //    }

        //}

        ////
        //// GET: /Post/Delete
        //[Authorize]
        //public ActionResult Delete(int id)
        //{
        //    var post = _storeDB.GetById(id);
        //    return View(post);
        //}

        ////
        //// GET: /Post/Delete
        //[HttpPost]
        //[Authorize]
        //public ActionResult Delete(Post post)
        //{
        //    var listComment = new CommentRepository().GetByPostId(post.Id);
        //    foreach (var item in listComment)
        //    {
        //        _storeDB.Delete(item.Id);
        //    }
        //    _storeDB.Delete(post.Id);
        //    return RedirectToAction("Index");
        //}

        ////
        //// GET: /Post/Details
        //[Authorize]
        //public ActionResult Details(int id)
        //{
        //    var post = _storeDB.GetById(id);
        //    return View(post);
        //}
        #endregion 

        #region ver_0.2 Database First
        //
        // GET: /Admin/  
        [Authorize]
        public ActionResult Index(int pageIndex = 1)
        {
            var posts = _storeDB.GetAll().OrderByDescending(t => t.CreatedDate).ThenByDescending(t => t.ModifyDate);

            ViewBag.TotalPage = (int)Math.Ceiling(posts.Count() / (double)Config.PageSize);
            ViewBag.PageIndex = pageIndex;
            ViewBag.PageSizeRender = Config.PageSizeRender;

            int take = Config.PageSize;
            int skipAt = (pageIndex * take) - take;

            return View(posts.Skip(skipAt).Take(take).ToList());
        }

        //
        // GET: /Admin/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        //
        // GET: /Post/Create
        [HttpPost]
        [Authorize]
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                _storeDB.Create(post);
                return RedirectToAction("Index");
            }
            else
            {
                return View(post);
            }
        }

        //
        // GET: /Post/Edit
        [Authorize]
        public ActionResult Edit(int id)
        {
            var post = _storeDB.GetOne(t => t.Id == id);
            post.Content = Server.HtmlDecode(post.Content);
            return View(post);
        }

        //
        // GET: /Post/Edit
        [HttpPost]
        //[ValidateInput(false)]
        [Authorize]
        public ActionResult Edit(Post post)
        {
            if (ModelState.IsValid)
            {
                var originalObj = _storeDB.GetOne(t => t.Id == post.Id);
                if (originalObj != null)
                {
                    originalObj.Title = post.Title;
                    originalObj.Content = post.Content;

                    _storeDB.Update(originalObj);
                }
                return RedirectToAction("Index");
            }
            else
            {
                return View(post);
            }

        }

        //
        // GET: /Post/Delete
        [Authorize]
        public ActionResult Delete(int id)
        {
            var post = _storeDB.GetOne(t => t.Id == id);
            return View(post);
        }

        //
        // GET: /Post/Delete
        [HttpPost]
        [Authorize]
        public ActionResult Delete(Post post)
        {            
            //delete comment
            CommentRepository _comment = new CommentRepository(_db);
            var listComment = _comment.GetMany(t => t.PostId == post.Id);
            foreach (var item in listComment)
            {
                _comment.Delete(item);
            }
            //delete post
            _storeDB.Delete(t => t.Id == post.Id);
            return RedirectToAction("Index");
        }

        //
        // GET: /Post/Details
        [Authorize]
        public ActionResult Details(int id)
        {
            var post = _storeDB.GetOne(t => t.Id == id);
            return View(post);
        }
        #endregion
    }
}
